package com.pounds.service.impl;

import com.pounds.mapper.CarMapper;
import com.pounds.mapper.UserMapper;
import com.pounds.pojo.Car;
import com.pounds.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

enum CarEnumError {
    Normal,
    DataNotFoundError,
    EmptyError,
    DatabaseError,
    DataError
}

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Car> getAllCar() {
        return carMapper.getAllCar();
    }

    @Override
    public List<Car> getUserCar(String account) {
        return carMapper.getUserCar(account);
    }

    @Override
    public void deleteCar(int id) {
        carMapper.deleteCar(id);
    }

    @Override
    public String addCar(String account,String name, String type, String level, String country, String power, String price) {
        if (account == null || name == null || type == null || level == null || country == null || power == null || price == null){
            return CarEnumError.DataError.toString();
        } else if (account.isEmpty() || name.isEmpty() || type.isEmpty() || level.isEmpty() || country.isEmpty() || power.isEmpty() || price.isEmpty()) {
            return CarEnumError.EmptyError.toString();
        }else {
            String userAccount = userMapper.findUserAccount(account);
            if (userAccount == null){
                return CarEnumError.DataNotFoundError.toString();
            } else if (userAccount.isEmpty()) {
                return CarEnumError.DatabaseError.toString();
            } else if (account.equals(userAccount)){
                carMapper.addCar(account, name, type, level, country, power, price);
                return CarEnumError.Normal.toString();
            }else{
                return CarEnumError.DatabaseError.toString();
            }
        }
    }
}
